home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cgazv5n5.arc
/
LIST9.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
4KB
|
148 lines
/*---------------------------------------- Listing 9 ------
* Intelligent use of goto's in error handling.
* See Listing 1 for copyright terms.
*-------------------------------------------------------*/
/* Read two adjacent 5-byte items each from two different
files, and compare them. What could be easier? */
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
/*---------------------------------------------------------
* NOTE: We are passing file handles to the functions and
* assume them to be valid. Otherwise, even more exception
* handling would be required!
*-------------------------------------------------------*/
void IsThisStructured ( FILE *File1, FILE *File2 );
void IsThisNot ( FILE *File1, FILE *File2 );
void IsThisNotToo ( FILE *File1, FILE *File2 );
void IsThisStructured ( FILE *File1, FILE *File2 )
{
char *Buf1, *Buf2;
int NumRead;
Buf1 = malloc ( 2 * 5 );
if ( Buf1 != NULL )
{
Buf2 = malloc ( 2 * 5 );
if ( Buf2 != NULL )
{
NumRead = fread ( Buf1, 5, 2, File1 );
if ( NumRead == 2 )
{
NumRead = fread ( Buf2, 5, 2, File2 );
if (NumRead == 2)
{
if ( memcmp ( Buf1, Buf2, 2 * 5 ))
printf ( "Items do not match!\n" );
else
printf ( "Items match\n" );
}
else
{
free ( Buf1 );
free ( Buf2 );
printf ( "I/O read error\n" );
}
}
else
{
free ( Buf1 );
free ( Buf2 );
printf ( "I/O read error\n" );
}
}
else
{
printf ( "Memory Allocation error\n" );
free ( Buf1 );
}
}
else
printf ( "Memory Allocation error\n" );
}
void IsThisNot ( FILE *File1, FILE *File2 )
{
char *Buf1, *Buf2;
int NumRead;
Buf1 = malloc ( 2 * 5 );
if ( Buf1 == NULL )
goto AllocFailed1;
Buf2 = malloc ( 2 * 5 );
if ( Buf2 == NULL )
goto AllocFailed2;
NumRead = fread ( Buf1, 5, 2, File1 );
if ( NumRead != 2 )
goto IoFailed;
NumRead = fread ( Buf2, 5, 2, File2 );
if ( NumRead != 2 )
goto IoFailed;
if ( memcmp ( Buf1, Buf2, 2 * 5 ))
printf ( "Items do not match!\n" );
else
printf ( "Items match\n" );
return; /* NEVER forget this, or you'll fall through ! */
AllocFailed2:
free ( Buf1 );
AllocFailed1:
printf ( "Memory Allocation error\n" );
return; /* no fall through */
IoFailed:
free ( Buf1 );
free ( Buf2 );
printf( "I/O read error\n" );
}
#define ExcCheck(ExcCondition, Label) \
if (ExcCondition) goto Label
void IsThisNotToo ( FILE *File1, FILE *File2 )
{
char *Buf1, *Buf2;
int NumRead;
Buf1 = malloc ( 2 * 5 );
ExcCheck ( Buf1 == NULL, AllocFailed1 );
Buf2 = malloc ( 2 * 5 );
ExcCheck ( Buf2 == NULL, AllocFailed2 );
NumRead = fread ( Buf1, 5, 2, File1 );
ExcCheck ( NumRead != 2, IoFailed );
NumRead = fread ( Buf2, 5, 2, File2 );
ExcCheck ( NumRead != 2, IoFailed );
if ( memcmp ( Buf1, Buf2, 2 * 5 ))
printf ( "Items do not compare!\n" );
else
printf( "Items compare\n" );
return; /* NEVER forget this, or you'll fall through */
AllocFailed2:
free ( Buf1 );
AllocFailed1:
printf ( "Memory Allocation error\n" );
return; /* Don't fall through */
IoFailed:
free ( Buf1 );
free ( Buf2 );
printf ( "I/O read error\n" );
}